package com.his.dictitem.service.Imp;

import com.his.dictitem.dao.DictItemMapper;
import com.his.dictitem.entity.*;
import com.his.dictitem.service.DictItemChargeService;
import com.his.dictitem.service.DictItemMedicineService;
import com.his.dictitem.service.DictItemPackageunitService;
import com.his.dictitem.service.DictItemServcie;
import com.his.dictitem.vo.*;
import com.his.login.entity.DictEmployee;
import com.xiao.common.util.BeanUtil;
import com.xiao.common.util.PinYin4jUtil;
import com.xiao.common.util.WorkIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Service
public class DictItemServiceImpl implements DictItemServcie {

    @Autowired
    private DictItemMapper dictItemMapper;

    @Autowired
    private DictItemMedicineService dictItemMedicineService;
    //@Autowired
    //private DictItemChargeService dictItemChargeService;
    @Autowired
    private DictItemPackageunitService dictItemPackageunitService;

    @Override
    public List<DictItem_ForMainshow> queryForMainShow(DictEmployee dictEmployee,DictItemSearchVo dictItemSearchVo) {
        dictItemSearchVo.setHospid(dictEmployee.getHOSPID());
        List<DictItem_ForMainshow> itemList=dictItemMapper.selectForMainShow(dictItemSearchVo);
        return itemList;
    }

    @Override
    public DictItem_Detailed queryDetailedItem(String itemid) {
        return dictItemMapper.selectDetailedItem(itemid);
    }

    @Transactional
    @Override
    public void insertItem(DictEmployee dictEmployee, DictItem_DetailedVo dictItemDetailedVo) {
        //                                   第一步：入参数据拆分
        //            传入数据包含：项目主信息、项目属性信息、项目价格列表、项目单位列表
        //项目主信息
        DictItem dictItem=BeanUtil.copyBean(dictItemDetailedVo,DictItem.class);
        //项目属性信息
        DictItemMedicineVo dictItemMedicineVo=dictItemDetailedVo.getDictItemMedicine();
        //项目价格列表 ---废弃  价格存主表
        //List<DictItemChargeVo> dictItemChargeVoList=dictItemDetailedVo.getDictItemCharges();
        //项目单位列表
        List<DictItemPackageunitVo> dictItemPackageunitVoList=dictItemDetailedVo.getDictItemPackageunits();

        //                                   第二步：插入数据
        //插入dict_item:项目主信息
        String itemid=WorkIdUtil.nextId();
        dictItem.setItemid(itemid);
        dictItem.setInputstr(PinYin4jUtil.converterToFirstSpell(dictItem.getItemname()));
        dictItem.setHospid(dictEmployee.getHOSPID());
        dictItem.setCreaterId(dictEmployee.getEMPID());
        dictItem.setCreaterName(dictEmployee.getEMPNAME());
        dictItem.setCreateTime(new Date());
        dictItemMapper.insert(dictItem);

        //插入 dict_item_medicine: 项目属性信息

        dictItemMedicineVo.setItemid(itemid);
        dictItemMedicineService.insert(dictEmployee,dictItemMedicineVo);

        //批量插入   dict_item_charge   项目价格信息 （这里按理只有一条）   废弃  价格存主表
//        for(DictItemChargeVo dictItemChargeVo :dictItemChargeVoList){
//            dictItemChargeVo.setItemid(itemid);
//            dictItemChargeService.insert(dictEmployee,dictItemChargeVo);
//        }

        //批量插入   dict_item_packageunit   项目单位信息
        for(DictItemPackageunitVo dictItemPackageunitVo:dictItemPackageunitVoList){
            dictItemPackageunitVo.setItemid(itemid);
            dictItemPackageunitService.insert(dictEmployee,dictItemPackageunitVo);
        }
    }

    @Override
    public List<DictItem> selectByEntity(DictItemVo dictItemVo) {
        DictItem dictItem=BeanUtil.copyBean(dictItemVo,DictItem.class);
        return dictItemMapper.selectByEntity(dictItem);
    }
}
